#include "CameraCorrect.h"
#include "ui_CameraCorrect.h"

CameraCorrect::CameraCorrect(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CameraCorrect)
{
    ui->setupUi(this);
    this->setWindowTitle("相机调整");
    display = new QCvDisplay;
    ui->hl_dis->addWidget(display);
    initUIs();
}

CameraCorrect::~CameraCorrect()
{
    delete ui;
}

void CameraCorrect::initUIs()
{
    ui->hs_exp->setStyleSheet("  \
                 QSlider::add-page:Horizontal\
                 {     \
                    background-color: rgb(87, 97, 106);\
                    height:4px;\
                 }\
                 QSlider::sub-page:Horizontal \
                {\
                    background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(231,80,229, 255), stop:1 rgba(7,208,255, 255));\
                    height:4px;\
                 }\
                QSlider::groove:Horizontal \
                {\
                    background:transparent;\
                    height:6px;\
                }\
                QSlider::handle:Horizontal \
                {\
                    height: 30px;\
                    width:8px;\
                    border-image: url(:/icons/camera.png);\
                    margin: -8 0px; \
                }\
                ");
      ui->hs_gain->setStyleSheet("  \
                   QSlider::add-page:Horizontal\
                   {     \
                      background-color: rgb(87, 97, 106);\
                      height:4px;\
                   }\
                   QSlider::sub-page:Horizontal \
                  {\
                      background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(231,80,229, 255), stop:1 rgba(7,208,255, 255));\
                      height:4px;\
                   }\
                  QSlider::groove:Horizontal \
                  {\
                      background:transparent;\
                      height:6px;\
                  }\
                  QSlider::handle:Horizontal \
                  {\
                      height: 30px;\
                      width:8px;\
                      border-image: url(:/icons/camera.png);\
                      margin: -8 0px; \
                  }\
                  ");

    float exposure = _sys_config.get<float>("camera.exposure");
    ui->cameraExposure->setText(QString::number(exposure));
    float cameraGain = _sys_config.get<float>("camera.gain");
    ui->cameraGain->setText(QString::number(cameraGain));

}

void CameraCorrect::on_pb_Grab_clicked()
{
    Millsecond t1 = std::chrono::steady_clock::now() ;
    CameraHelper::GetInstance()->_camera_ptr->StartGrabbing();
    Millsecond t2 = std::chrono::steady_clock::now() ;
    std::cout<<"Grab Time:"<<(t2 -t1).count()<<std::endl;

    auto mat = CameraHelper::GetInstance()->GetPictrue();
    Millsecond t4 = std::chrono::steady_clock::now() ;
    std::cout<<"All Time:"<<(t4 -t1).count()<<std::endl;
    //if  ((t2 - t1)  > MillDiff(20)) {
    display->display(mat);
}

void CameraCorrect::on_pb_Take_clicked()
{
    Millsecond t1 = std::chrono::steady_clock::now() ;
    auto mat = CameraHelper::GetInstance()->GetPictrue();
    Millsecond t2 = std::chrono::steady_clock::now() ;
    std::cout<<"Take Time:"<<(t2 -t1).count()<<std::endl;
}

void CameraCorrect::on_pb_stop_clicked()
{
    CameraHelper::GetInstance()->_camera_ptr->StopGrabbing();
}

void CameraCorrect::on_pb_CamImp_clicked()
{
    // 相机
    float gain = ui->cameraGain->text().toFloat() ;
    int exposure = ui->cameraExposure->text().toInt() ;
//    auto roi_offset_w = ui->roi_offset_w->text().toFloat();
//    auto roi_offset_h = ui->roi_offset_h->text().toFloat();
    int roi_width = ui->roi_width->text().toInt();
    int roi_height = ui->roi_height->text().toInt();

//    _sys_config.put("camera.exposure", exposure) ;
//    _sys_config.put("camera.gain", gain) ;
////    _sys_config.put("camera.roi_offset_x", roi_offset_w);
////    _sys_config.put("camera.roi_offset_y", roi_offset_h);
//    _sys_config.put("camera.roi_width", roi_width);
//    _sys_config.put("camera.roi_height", roi_height);
}

void CameraCorrect::on_hs_exp_valueChanged(int value)
{
    int expose = value * 200000 / 100;
    ui->cameraExposure->setText(QString::number(expose));
}

void CameraCorrect::on_hs_gain_valueChanged(int value)
{
    int gain = value * 60 / 100;
    ui->cameraGain->setText(QString::number(gain));
}

void CameraCorrect::on_pb_showGrab_clicked()
{
    if(!camShow.load()){
        camShow.exchange(true);
        std::thread *back = new std::thread([this](){
            while(camShow.load() && currentStatus.GetInit(MU_DEVICE_CAM) ){
                auto mat = CameraHelper::GetInstance()->GetPictrue();
                display->display(mat);
                sleep_s(1);
            }
        });
        back->detach();
    }
}

void CameraCorrect::on_pb_close_clicked()
{
    camShow.store(false);
}
